package insight.google.game.era.core;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import insight.google.game.era.bo.GameRoom;
import insight.google.game.era.bo.GameUser;
import insight.google.game.era.bo.Room;

import insight.google.game.era.bo.card.GameCard;
import insight.google.game.era.data.AppDataManagerFactory;
import insight.google.game.era.data.GameUserDataManager;
import insight.google.game.era.data.RoomDataManager;
import insight.google.game.era.data.AppDataManagerFactory.DataManager;

import insight.google.game.era.data.TransactionHandler;
import insight.google.game.era.data.User;

import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.ScriptProxy;

import insight.google.game.era.data.GameUserDataManager;

public class Util {

	private Util() {

	}

	public static GameRoom getRoom() {
		GameCenterManager gameCenter = GameCenterManager.getInstance();
		return (GameRoom) gameCenter.getRoomByUser();
	}

	public static ScriptSession getUserScriptSession(String user) {
		WebContext webContext = WebContextFactory.get();
		webContext.getSession().setAttribute("user", user);
		ScriptSession scriptSession = webContext.getScriptSession();
		scriptSession.setAttribute("user", user);
		return scriptSession;// new ScriptProxy(scriptSession);
	}

	public static User getUserBySession(TransactionHandler txHandler) {
		if(txHandler == null){
			txHandler = Util.createTranscationHandler();
		}
		return UserSessionManager.getInstance(txHandler).getUserBySession();
	}
	
	public static User getUserBySession() {
		
		return UserSessionManager.getInstance(null).getUserBySession();
	}
	
	
	public static TransactionHandler createTranscationHandler(){
		return new TransactionHandler(); 
	}

//	public static GameRoom findRoomByName(String roomName) {
//		GameCenter gameCenter = GameCenter.getInstance();
//		return gameCenter.getRoomByName(roomName);
//	}
//	public static GameRoom getRoomFromCache(String roomName) {
//		GameCenter gameCenter = GameCenter.getInstance();
//		return gameCenter.getRoomByName(roomName);
//	}	
//	

	public static GameUser getUserByRoom(GameRoom room) {
//		GameUser currUser = null;
//		Object sessionAttr =   UserSessionManager.getInstance().getSessionAttributeByName(UserSessionManager.ATTR_USER);
//		System.out.println("Util.getUserByRoom  sessionAttr  ---   "+ sessionAttr);
//		if (sessionAttr != null) {
//			System.out.println("Util.getUserByRoom  rooom  ---   "+ room.getUserList());
//			currUser = room.getUserByName((String) sessionAttr);
//			System.out.println("Util.getUserByRoom  user   ---   "+ currUser);
//			if (currUser != null){
//				currUser.setScriptProxy(UserSessionManager.getInstance().getNewScripProxy(currUser.getName()));
//			}
//		}
//		if(room.isUserExist(currUser.getName())){
//			return currUser;
//		}
		GameUser currUser = null;
		WebContext webContext = WebContextFactory.get();
		Object sessionAttr = webContext.getSession().getAttribute("user");
		if (sessionAttr != null) {
			//currUser = room.getUserByName((String) sessionAttr);
			if (currUser != null)
				currUser.setScriptProxy(new ScriptProxy(Util
						.getUserScriptSession(currUser.getName())));

		}
		return currUser;
	}

	public static SimpleDateFormat getDateFormat() {
		return new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
	}

	public static String dateToString(Date date) {
		return Util.getDateFormat().format(date);
	}

	public static void shuffleCards(List a, int shuffleDepth) {
		int n = a.size();
		Random random = new Random();
		random.nextInt();
		for (int i = 0; i < n; i++) {
			int change = i + random.nextInt(n - i);
			Object helper = a.get(i);
			a.set(i, a.get(change));
			a.set(change, helper);

			// swap(a, i, change);
		}
		// if(shuffleDepth >1) shuffleCards(a, shuffleDepth--);
	}
	
	public static GameUser getUserByName(String userName) {
		return ((GameUserDataManager)AppDataManagerFactory.getInstance().
					getDataManager(DataManager.GAMEUSER_DATA_MANAGER.toString(),Util.createTranscationHandler()))
					.findByName(userName);
	}

	public static GameUser getUserByNameAndRoom(GameRoom room, User user) {
		List<GameUser> userList = room.getUserList();
		for(GameUser gameUser : userList){
			if(user.getName().equals(gameUser.getName())){
				gameUser.setScriptProxy(user.getScriptProxy());
				return gameUser;
			}
		}
		return null;
	}

	public static List<String> converGameUserListToString(List<GameUser> gameUserList){
		List<String> userList = new ArrayList<String>();
		for(GameUser user : gameUserList){
			userList.add(user.getName());
		}
		return userList;
	}
	
	public static List<GameUser> converStringToGameUserList(List<String> userList){
		List<GameUser> gameUserList = new ArrayList<GameUser>();
		for(String userName : userList){
			GameUser gameUser = Util.getUserByName(userName);
			gameUserList.add(gameUser);
		}
		return gameUserList;
	}
	
	public static List<GameCard> converStringToGameCardList(List<String> gameCardIdList){
		if(gameCardIdList != null && !gameCardIdList.isEmpty()){
			GameUserDataManager gameUserDataManager = (GameUserDataManager)AppDataManagerFactory.getInstance().getDataManager(DataManager.GAMEUSER_DATA_MANAGER.toString(),Util.createTranscationHandler());
			List<GameCard> cardList = gameUserDataManager.findAllCardsByIds(gameCardIdList);
			if(cardList != null){
				return new ArrayList<GameCard>(cardList);
			}
		}
		return null;
	}
	
	public static GameRoom getUserCurrentRoom(GameUser user){
		RoomDataManager roomDataManager = (RoomDataManager) AppDataManagerFactory.getInstance().getDataManager(DataManager.ROOM_DATA_MANAGER.toString(),Util.createTranscationHandler());
		return roomDataManager.findByName(user.getCurrentRoom().getRoomName());
	}
	
	
	public static String getCurrentTime(){
		return new Date().toString();
	}
}
